function createApiKeyButton() {
	$( document.createElement('a') )
		.html( "Get API Key")
		.click(function() {
			getApiKey(function(apiKey) {
				alert(apiKey);
			})
		})
		.addClass('topmenu')
		.prependTo(document.body)
	;
}

/* Get the users current API Key */
function getApiKey(callback) {
	var extractKeyFromHTML = function(resp) {
		return resp.match(/[^><]*(?=<\/b><BR><BR><input type=submit class=button name=genhapikey)/);
	}
	
	var throwAPIKeyException = function() {
		alert( 'Oh Shit! The api key fetch broke somehow. Contact bastard!');
	}
	
	captchaAwareAjax({
	    url: '/servlet/Preferences',
	    success: function( resp, textStatus ) {
	        apiKey = extractKeyFromHTML(resp);
	        if( apiKey.length == 0 )
	        	throwAPIKeyException();
	        else {
	            apiKey = apiKey[0].replace(/\s*/,'');
	            if( apiKey.match(/\[.*?\]/) ) {
	            	captchaAwareAjax({
	                	url: '/servlet/Preferences',
	                	method: 'POST',
	                	data: {
	                		genhapikey:	'Generate new key'
	                	},
	                	success: function(resp, textResponse) {
	            	        apiKey = extractKeyFromHTML(resp);
	            	        if( apiKey.length == 0 )
	            	        	throwAPIKeyException();
	            	        else {
	            	        	callback(apiKey);
	            	        }
	                	}
	                })
	            } else {
	            	callback(apiKey);
	            }
	        }
	    }
	});
}